package ch.ethz.fcl.metrobuzz.algorithm.voronoi;

import java.awt.Color;

import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.algorithm.network.shortest_path.public_transport.Walk;
import ch.ethz.fcl.metrobuzz.algorithm.voronoi.interpolation.InterpolationVoronoiCell;
import ch.ethz.fcl.metrobuzz.render.techniques.colors.MBColorMap;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.tools.MBGL;

/**
 * Create voronoi cell particularly for accessibility visualization Flexible to
 * extend any of the specific voronoi algorithms
 * 
 * @author Zeng Wei
 * @email zeng@arch.ethz.ch
 * 
 */
public class AccessibilityVoronoiCell extends InterpolationVoronoiCell {
	public AccessibilityVoronoiCell(float x, float y, float weight) {
		super(x, y, weight);
	}

	public AccessibilityVoronoiCell(Point2D p, float weight) {
		super(p, weight);
	}

	public void render(MBContainer container, MBColorMap colorMap,
			float timeLimit) {
		float depth = 1;

		float startTime = timeLimit - weight;

		float innerRadius = 0, outerRadius = Walk.getWalkScreenLength(
				container.getData(), weight);

		float inDepth = weight / timeLimit * depth;
		float outDepth = 0;

		Color inColor = colorMap.getColor(startTime, 0, 90);
		Color outColor = colorMap.getColor(timeLimit - 1, 0, 90);

		render(innerRadius, inDepth, inColor, outerRadius, outDepth, outColor);

		// draw a circle around the location
		float walkDist = Walk.getWalkScreenLength(container.getData(), weight);
		GL11.glColor4f(0, 0, 0, 1);
		GL11.glLineWidth(1.0f);
		MBGL.renderEclipse(center.getX(), center.getY(), 0, walkDist, walkDist,
				true);
		MBGL.renderEclipse(center.getX(), center.getY(), 0, walkDist * 1.2f,
				walkDist * 1.2f, true);

		// draw a dot at the location
		GL11.glPointSize(1.0f);
		GL11.glBegin(GL11.GL_POINTS);
		GL11.glVertex3f(center.getX(), center.getY(), depth);
		GL11.glEnd();

		GL11.glBegin(GL11.GL_LINES);
		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(center.getX(), center.getY(), 0);

		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(center.getX() + outerRadius, center.getY(), 0);
		GL11.glEnd();
	}

	protected void render(float innerRadius, float inDepth, Color innerColor,
			float outerRadius, float outDepth, Color outerColor) {
		GL11.glBegin(GL11.GL_TRIANGLE_STRIP);

		int slices = 100;
		for (int i = 0; i <= slices; i++) {
			float inX = (float) center.getX();
			float inY = (float) center.getY();
			GL11.glColor4f(innerColor.getRed() / 255.0f,
					innerColor.getGreen() / 255.0f,
					innerColor.getBlue() / 255.0f, 1);

			GL11.glColor4f(0.2f, 0.2f, 0.8f, 1);
			GL11.glVertex3f(inX, inY, inDepth);

			float outX = (float) (center.getX() + outerRadius
					* Math.cos(Math.PI * 2 / slices * i));
			float outY = (float) (center.getY() + outerRadius
					* Math.sin(Math.PI * 2 / slices * i));
			// GL11.glColor4f(outerColor.getRed() / 255.0f,
			// outerColor.getGreen() / 255.0f,
			// outerColor.getBlue() / 255.0f, 1);

			GL11.glColor4f(0.2f, 0.2f, 0.8f, 1);
			GL11.glVertex3f(outX, outY, outDepth);
		}

		GL11.glEnd();
	}
}
